##
# This class handles the activations of the exams
class ActivationController < ApplicationController
	helper :paginator
	helper :right
	include PaginatorHelper
	include RightHelper
	layout 'scaffold'

  # verifies that only the owner of an exam can show, edit, and destroy an 
  # activation
	before_filter :verify_owner, :only => [:show, :edit, :destroy, :update]
  # verifies the neccessary rights to access this controller
	before_filter :verify_teacher

  ##
  # This method redirects to the list-method
  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }
	# Session must be active
	verify :session => 'user',
				 :redirect_to => { :controller => 'user', :action => :login },
				 :add_flash => { :error => 'you must be logged in to create activations'}

  ## 
  # lists all activations
  def list
		exams = Exam.find(:all, :conditions => {:user_id => session['user']})
		@activations = Array.new
		exams.each { |e| @activations.concat e.activations }
		@activation_pages, @activations = paginate_collection @activations, 
																													:per_page => 10,
                                                          :page => params[:page]
  end

  ##
  # shows an activation
  def show
    @activation = Activation.find(params[:id])
  end

  ##
  # creates an activation-creation-form
  def new
    @activation = Activation.new
  end

  ##
  # stores an activation in the DB
  def create
		logger.info params[:activation][:start]
    @activation = Activation.new(params[:activation])
		@activation.exam = Exam.find(params[:exams])
		@activation.group = Group.find(params[:groups])
    if @activation.save
      flash[:notice] = 'Activation was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

  ##
  # creates an edit-form
  def edit
    @activation = Activation.find(params[:id])
		@selected_exam = @activation.exam.id
		@selected_group = @activation.group.id
  end

  ##
  # updates an activation-row in the DB
  def update
    @activation = Activation.find(params[:id])
		@activation.exam = Exam.find(params[:exams])
		@activation.group = Group.find(params[:groups])
    if @activation.update_attributes(params[:activation])
      flash[:notice] = 'Activation was successfully updated.'
      redirect_to :action => 'show', :id => @activation
    else
      render :action => 'edit'
    end
  end

  ##
  # deletes an activation from the DB
  def destroy
    Activation.find(params[:id]).destroy
    redirect_to :action => 'list'
  end

	private
  ##
  # Verifies that the currently logged in user is the owner of the activation
  # in the GET-variable.
	def verify_owner
		id = params[:id]
		act = Activation.find(id)
		if act.exam.user != session['user']
			flash[:error] = "Sorry, editing activations " \
											"from foreign users isn't allowed"
			redirect_to :action => 'list'
		end
	end
end
